﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CodeGame
{
    internal class ConsoleLine
    {
        public Color LineColor;
        public string Line;

        public ConsoleLine(string line, Color color)
        {
            if (line.Length > ConfigManager.WindowWidth / 10)
            {
                line = line.Substring(0, ConfigManager.WindowWidth / 10);
            }
            this.Line = line;
            this.LineColor = color;
        }
    }

    internal class LuaConsole : UIItem
    {

        private string CurrentInput = "";
        private ConsoleLine[] ConsoleLines;
        private int ConsoleCount;

        private bool FullConsole = false;
        private bool ShowConsole = true;

        public LuaConsole()
        {
            this.ConsoleLines = new ConsoleLine[ConfigManager.WindowHeight / 18];
            this.ConsoleCount = 0;
        }

        public void Print(string line, Color col)
        {
            this.Print(line, col, true);
        }

        public void Print(string line, Color col, bool first)
        {
            if (line.Contains('\n'))
            {
                if (first)
                {
                    foreach (string item in line.Split('\n'))
                    {
                        this.Print(item, col, false);
                    }
                }
                return;
            }

            if (this.ConsoleCount < this.ConsoleLines.Length)
            {
                this.ConsoleLines[this.ConsoleCount] = new ConsoleLine(line, col);
                this.ConsoleCount++;
            }
            else
            {
                for (int i = 1; i < this.ConsoleLines.Length; i++)
                {
                    this.ConsoleLines[i - 1] = this.ConsoleLines[i];
                }
                this.ConsoleLines[this.ConsoleLines.Length - 1] = new ConsoleLine(line, col);
            }
        }

        public override void Update()
        {
            base.Update();

            if (this.KeyboardInfo.IsKeyDown(Keys.OemTilde) && this.IsFirstPress(Keys.OemTilde))
            {
                this.ToggleShow();
            }

            if (!this.ShowConsole)
            {
                this.PostDown();
                return;
            }

            /*
            foreach (Keys item in this.KeyboardInfo.GetPressedKeys())
            {
                Console.WriteLine(item.ToString());
            }
            */

            switch (this.GetKeyboardChar())
            {
                case "ENTER":
                    if (this.CurrentInput != "")
                    {
                        this.Print("- " + this.CurrentInput, Singletons.OldEntryColor);
                        try
                        {
                            Singletons.Scripting.DoString(this.CurrentInput);
                        }
                        catch (Exception ex)
                        {
                            this.Print(ex.Message, Singletons.ErrorText);
                        }
                        this.CurrentInput = "";
                    }
                    break;
                case "BACKSPACE":
                    if (this.CurrentInput.Length > 0)
                    {
                        this.CurrentInput = this.CurrentInput.Substring(0, this.CurrentInput.Length - 1);
                    }
                    break;
                case "TAB":
                    if (this.CurrentInput.Length < (ConfigManager.WindowWidth / 10) - 4)
                    {
                        this.CurrentInput += "    ";
                    }
                    break;
                default:
                    if (this.CurrentInput.Length < (ConfigManager.WindowWidth / 10))
                    {
                        this.CurrentInput += this.GetKeyboardChar();
                    }
                    break;
            }

            this.PostDown();
        }

        public override void Draw()
        {
            if (!this.ShowConsole)
            {
                return;
            }

            int x = 0;

            if (this.FullConsole)
            {
                Singletons.Content.DrawRectangle(new Rectangle(10, 10, ConfigManager.WindowWidth - 20, (this.ConsoleLines.Length * 15) + 50), Singletons.WindowColor);
                foreach (ConsoleLine item in this.ConsoleLines)
                {
                    if (item != null)
                    {
                        Singletons.Content.DrawText("console", item.Line, 20, x * 15 + 20, item.LineColor);
                    }
                    x++;
                }
            }
            else
            {
                Singletons.Content.DrawRectangle(new Rectangle(10, 10, ConfigManager.WindowWidth - 20, 15 * 15 + 50), Singletons.WindowColor);
                if (this.ConsoleCount > 15)
                {
                    for (int i = this.ConsoleCount - 15; i < this.ConsoleCount; i++)
                    {
                        if (this.ConsoleLines[i] != null)
                        {
                            Singletons.Content.DrawText("console", this.ConsoleLines[i].Line, 20, x * 15 + 20, this.ConsoleLines[i].LineColor);
                        }
                        x++;
                    }
                }
                else
                {
                    for (int i = 0; i < 15; i++)
                    {
                        if (this.ConsoleLines[i] != null)
                        {
                            Singletons.Content.DrawText("console", this.ConsoleLines[i].Line, 20, x * 15 + 20, this.ConsoleLines[i].LineColor);
                        }
                        x++;
                    }
                }
            }

            Singletons.Content.DrawText("console", "> " + this.CurrentInput, 20, (x + 1) * 15 + 20, Singletons.EntryText);

            base.Draw();
        }

        public void Toggle()
        {
            this.FullConsole = !this.FullConsole;
        }

        public void ToggleShow()
        {
            this.ShowConsole = !this.ShowConsole;
            Singletons.IsKeyboardLocked = this.ShowConsole;
        }
    }
}
